package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _68二叉树的最大深度
 * @description
 * @date 2023/07/30 14:55
 * <p>
 * ******************************************************
 */
public class _68二叉树的最大深度 {

    int max = 0;

    public int maxDepth(TreeNode root) {
        foreach(root, 0);
        return max;
    }

    private void foreach(TreeNode root, int depth) {
        if (root == null) {
            max = Math.max(max, depth);
            return;
        }

        foreach(root.left, depth + 1);
        foreach(root.right, depth + 1);

    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
